<?php
$data = array();

$job_id = $_GET['job_id'];

$update = array(
        job_name => $_POST['job_name'],
	job_scoretop => $_POST['job_scoretop'],
        job_detail => $_POST['job_detail'],
        job_date => $_POST['job_date'].' '.$_POST['h'].':'.sprintf('%02d',$_POST['m']).':00',
        sec_id => $_POST['sec_id']
);

$data['success'] = false;
$where = "job_id = '{$job_id}'";
if(!empty($_FILES['job_file'])){
    //ลบไฟล์เก่าทิ้ง
    $sql = "SELECT job_file FROM tb_job WHERE {$where}";
    $fetch = mysql_fetch_assoc(mysql_query($sql));
    if(file_exists('job/'.$fetch['job_file'])) @unlink('job/'.$fetch['job_file']);
    //กำหนดข้อมูลไฟล์ใหม่
    $file_name = explode('.',$_FILES['job_file']['name']);
    $file_name = $job_id.'.'.array_pop($file_name);
    $data['job_file'] = $file_name;
    if(copy($_FILES['job_file']['tmp_name'],'job/'.$file_name)){
        if($dbfunc->update("tb_job",$update,$where)){
            $data['success'] = true;
            $data['update'] = $update;
        }
    }
}
else{
    if($dbfunc->update("tb_job",$update,$where)){
        $data['success'] = true;
        $data['update'] = $update;
    }
}

echo json_encode($data);
